package com.ruikong.dazhi.service.impl.base;

import com.ruikong.dazhi.controller.result.BaseResult;
import com.ruikong.dazhi.dao.base.PersonnelMapper;
import com.ruikong.dazhi.dao.base.RoleMapper;
import com.ruikong.dazhi.entity.base.Personnel;
import com.ruikong.dazhi.entity.base.Role;
import com.ruikong.dazhi.model.input.ChangePasswordInput;
import com.ruikong.dazhi.model.output.PersonnelOutput;
import com.ruikong.dazhi.service.base.ILoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @program: dailychemicalmes
 * @description:
 * @author: tybengine
 * @create: 2020-11-30 14:36
 **/
@Service
public class LoginServiceImpl implements ILoginService {

    @Autowired
    private PersonnelMapper personnelMapper;
    @Autowired
    private RoleMapper roleMapper;


    public int checkLogin(String account, String password, BaseResult result, HttpSession httpSession, HttpServletRequest request) throws UnsupportedEncodingException {
        if(StringUtils.isNotBlank(account)){
            Personnel personnelInfo = personnelMapper.getPersonnelByAccount(account);
            if(personnelInfo !=null){
                Personnel item = personnelInfo;
                if(StringUtils.isNotBlank(item.getPassword()) && item.getPassword().equals(DigestUtils.md5DigestAsHex(password.getBytes("utf-8")))){
                    if(item.getIsUsed() != 1 ){
                        result.setMsg("该账号未启用");
                        return 0;
                    }else {
                        httpSession.setAttribute("userId", item.getPersonnelId());
                        httpSession.setAttribute("personnel", item);

                        // 登录人员信息存入session中
                        Role role = roleMapper.getByRoleId(item.getRoleId());
                        PersonnelOutput personnelOutput = new PersonnelOutput();
                        personnelOutput.setPersonnelId(item.getPersonnelId());
                        personnelOutput.setDeleted(item.isDeleted());
                        personnelOutput.setCreateTime(item.getCreateTime());
                        personnelOutput.setUpdateTime(item.getUpdateTime());
                        personnelOutput.setPersonnelName(item.getPersonnelName());
                        personnelOutput.setPersonnelNumber(item.getPersonnelNumber());
                        personnelOutput.setAccount(item.getAccount());
                        personnelOutput.setRoleId(item.getRoleId());
                        personnelOutput.setRoleName(role.getRoleName());
                        personnelOutput.setUserId(item.getUserId());
                        httpSession.setAttribute("outPersonnel", personnelOutput);

                        // 登录信息存入日志

                        return 1;
                    }
                } else {
                    result.setMsg("登录密码不正确！");
                    return 0;
                }
            } else {
                result.setMsg("该登录账号不存在！");
                return 0;
            }
        } else {
            result.setMsg("请输入登录账号！");
            return 0;
        }
    }

    public void loginOut(HttpSession httpSession, HttpServletRequest request){
        // 退出信息存入日志


        httpSession.invalidate();
    }

    @Override
    public int changePassword(BaseResult result, ChangePasswordInput changePasswordInput) {
        Personnel personnelInfo = personnelMapper.getByPersonnelId(changePasswordInput.getPersonnelId()!=null?changePasswordInput.getPersonnelId():-1);
        if(personnelInfo != null){
            if(StringUtils.isNotBlank(personnelInfo.getPassword()) && personnelInfo.getPassword().equals(DigestUtils.md5DigestAsHex(changePasswordInput.getOldPassword().getBytes(StandardCharsets.UTF_8)))){
                if(StringUtils.isNotBlank(changePasswordInput.getNewPassword()) && changePasswordInput.getNewPassword().equals(changePasswordInput.getNewPassword2())){
                    Personnel info = new Personnel();
                    info.setAccount(changePasswordInput.getAccount());
                    info.setPassword(DigestUtils.md5DigestAsHex(changePasswordInput.getNewPassword().getBytes(StandardCharsets.UTF_8)));
                    info.setPersonnelId(changePasswordInput.getPersonnelId());

                    personnelMapper.updatePassword(info);
                    result.setMsg("修改成功！");
                    return 1;
                } else {
                    result.setMsg("两次输入密码不一致！");
                    return 0;
                }
            } else {
                result.setMsg("输入旧密码不准确！");
                return 0;
            }
        } else {
            result.setMsg("用户不存在!");
            return 0;
        }
    }

}
